package a08_回溯算法;

import java.util.*;

/**
 * <p>
 * a14_递增子序列
 * </p>
 *
 * @author flyduck
 * @since 2025-01-04
 */
public class a14_递增子序列 {
    public static void main(String[] args) {
        int[] nums= new int[]{4,7,6,7};
        a14_递增子序列 test = new a14_递增子序列();
        List<List<Integer>> subsequences = test.findSubsequences(nums);
        System.out.println(subsequences);
    }

    public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> path = new LinkedList<>();

        traversal(result, path, nums,0);
        return result;
    }

    private void traversal(List<List<Integer>> result,
                           LinkedList<Integer> path,
                           int[] nums,
                           int startIdx){
        if(path.size() >= 2){
            result.add(new ArrayList<>(path));
        }
        if (startIdx == nums.length) {
            return;
        }
        Set<Integer> set = new HashSet<>();//对当前层去重使用，每一层都有自己的set
        for (int i = startIdx; i < nums.length; i++) {
            if(path.size() > 0 && nums[i] < path.get(path.size() - 1)){
                continue;
            }
            if(set.contains(nums[i])){
                continue;
            }
            path.add(nums[i]);
            set.add(nums[i]);
            traversal(result, path, nums,i + 1);
            path.removeLast();
        }
    }
}
